type CallbackPlacement = 'before' | 'after'
function useModal(option?: {
  closeCallbackPlacement?: CallbackPlacement
  closeCallback?: () => boolean
  openCallbackPlacement?: CallbackPlacement
  openCallback?: () => boolean
}) {
  const visible = ref(false)

  function setVisible(bol: boolean) {
    visible.value = bol
  }

  function openModal() {
    if (option && (option.openCallbackPlacement === 'before' || option.openCallback)) {
      if (option.openCallback) {
        let bol = option.openCallback()
        if (!bol) {
          return
        }
      }
    }

    setVisible(true)

    if (option && option.openCallbackPlacement === 'after') {
      if (option.openCallback) {
        let bol = option.openCallback()
        if (!bol) {
          return
        }
      }
    }
  }

  function closeModal() {
    if (option && (option.closeCallbackPlacement === 'before' || option.closeCallback)) {
      if (option.closeCallback) {
        let bol = option.closeCallback()
        if (!bol) {
          return
        }
      }
    }

    setVisible(false)

    if (option && option.closeCallbackPlacement === 'after') {
      if (option.closeCallback) {
        let bol = option.closeCallback()
        if (!bol) {
          return
        }
      }
    }
  }

  return {
    openModal,
    closeModal,
    visible
  }
}

export default useModal
